package settings;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Settings
{
	private static HashMap<String, String> properties = new HashMap<String, String>();

	public static void init() throws IOException
	{
		try
		{
			FileInputStream in = new FileInputStream("settings.txt");
			byte[] buf = new byte[in.available()];
			in.read(buf, 0, in.available());
			String file = new String(buf);
			String[] lines = file.split("\\n");
			for (int i = 0; i < lines.length; i++)
			{
				String[] line = lines[i].split(" ?= ?");
				properties.put(line[0],line[1].trim());
			}
			in.close();
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static String get(String key)
	{
		return properties.get(key);
	}
	public static void set(String key, String value) throws IOException
	{
		properties.put(key, value);
		save();
	}
	private static void save() throws IOException
	{
		try
		{
			BufferedWriter out = new BufferedWriter(new FileWriter("settings.txt"));
			for(Map.Entry<String, String> entry : properties.entrySet()) {
			    out.write(entry.getKey()+" = "+entry.getValue());
			    out.newLine();
			}
			out.close();
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
